package com.zx.service;

import com.zx.bean.Express;

import com.zx.dao.ExpressDao;
import com.zx.dao.impl.ExpressDaoImpl;
import com.zx.exception.DuplicateCodeException;
import com.zx.util.RandomUtil;

import java.util.List;
import java.util.Map;


public class ExpressService {
     private static ExpressDao dao=new ExpressDaoImpl();

    public static  List<Map<String, Integer>> console() {
        return dao.console();
    }

    public static List<Express> findAll(boolean limit, int offset, int pageNumber) {
        return dao.findAll(limit,offset,pageNumber);
    }


    public static Express findByNumber(String number) {
        return dao.findByNumber(number);
    }


    public static  Express findByCode(String code) {
        return dao.findByCode(code);
    }

    public static List<Express> findByUserPhone(String userPhone) {
        return dao.findByUserPhone(userPhone);
    }


    public static List<Express> findBySysPhone(String sysPhone) {
        return dao.findBySysPhone(sysPhone);
    }


    public static boolean insert(Express e)  {
        //之前的插入是没有取件码的, 所以我们要随机生成取件码
        e.setCode(RandomUtil.getCode());
        try {


            return dao.insert(e);
        } catch (DuplicateCodeException duplicateCodeException) {
            return insert(e);
        }
    }


    public static boolean update(int id, Express newExpress) {
        Express byNumber = dao.findByNumber(newExpress.getNumber());
        if(newExpress.getStatus()==1){
            updateStatus(byNumber.getCode());
        }
        return dao.update(id, newExpress);

    }


    public static boolean delete(int id) {
        return dao.delete(id);
    }

    public static boolean updateStatus(String code) {
        return dao.updateStatus(code);
    }
}
